
package com.sm.shoplook.service.suit.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sm.shoplook.constant.ResultMessage;
import com.sm.shoplook.dao.mapper.suit.SuitScanRecordMapper;
import com.sm.shoplook.domain.suit.Suit;
import com.sm.shoplook.domain.suit.SuitScanRecord;
import com.sm.shoplook.service.suit.ApiSuitScanRecordService;
import com.sm.shoplook.service.suit.ApiSuitService;
import com.sm.shoplook.vo.base.ApiException;
import com.sm.shoplook.vo.base.ApiResult;
import com.sm.shoplook.vo.suit.front.resp.ApiSuitReqVo;

/**
 * @author chenpy
 * @desc 套装浏览收藏
 * @date 2018/8/21 16:56
 * @see
 */
@Service
public class ApiSuitScanRecordServiceImpl implements ApiSuitScanRecordService {

	private static final Logger LOGGER = LoggerFactory.getLogger(ApiSuitScanRecordServiceImpl.class);
	@Autowired
	private SuitScanRecordMapper suitScanRecordMapper;
	@Autowired
	private ApiSuitService apiSuitService;

	@Override
	public ApiResult<Integer> updateSuit(ApiSuitReqVo suitReqVo) throws ApiException {
		ApiResult<Suit> suitApiResult = apiSuitService.selectSuitById(suitReqVo.getSuitId());
		Suit dbSuit = suitApiResult.getData();
		if (dbSuit == null) {
			return new ApiResult<>(ResultMessage.RESULT_ERROR_0);
		}
		Suit suit = new Suit();
		suit.setId(dbSuit.getId());
        SuitScanRecord suitScanRecord = new SuitScanRecord();
        suitScanRecord.setCustomerId(suitReqVo.getCustomerId());
        suitScanRecord.setSuitId(suitReqVo.getSuitId());
        suitScanRecord.setType(suitReqVo.getType());
		// 收藏
		if (suitReqVo.getType() == 1) {
            // 判断是否已经收藏过
            int count = suitScanRecordMapper.selectCount(suitScanRecord);
            // 已经收藏过
            if (count > 0) {
                return new ApiResult<>(ResultMessage.SUIT_RECORD_IS_COLLECTED);
            }
            suit.setCollectCount(dbSuit.getCollectCount() + 1);
		}
		// 浏览
		else if (suitReqVo.getType() == 2) {
			suit.setScanCount(dbSuit.getScanCount() + 1);
		}
		try {
			apiSuitService.saveSuit(suit);
		} catch (ApiException e) {
			e.printStackTrace();
			LOGGER.error("e: {}", e);
			throw new ApiException(ResultMessage.RESULT_ERROR_0);
		}
		suitScanRecord.setCreateTime(new Date());
		suitScanRecordMapper.insertSelective(suitScanRecord);
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
	}

	@Override
	public ApiResult<List<SuitScanRecord>> selectSuitScanRecordByCustomrId(Integer customerId) {
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1,
				suitScanRecordMapper.selectSuitScanRecordByCustomrId(customerId));
	}

	@Override
	public ApiResult<Integer> getCollectSuitCond(Integer customerId, Integer suitId) {
		return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1,
				suitScanRecordMapper.getCollectSuitCond(customerId, suitId));
	}

    @Override
    public ApiResult removeCollect(Integer suitId, Integer customerId) throws ApiException {
	    SuitScanRecord suitScanRecord = new SuitScanRecord();
	    suitScanRecord.setSuitId(suitId);
	    suitScanRecord.setType(1);
	    suitScanRecord.setCustomerId(customerId);
	    suitScanRecordMapper.delete(suitScanRecord);
        ApiResult<Suit> suitApiResult = apiSuitService.selectSuitById(suitId);
        Suit dbSuit = suitApiResult.getData();
        if (dbSuit == null || dbSuit.getCollectCount() < 1) {
            return new ApiResult<>(ResultMessage.RESULT_SUCCESS_1);
        }
        dbSuit.setCollectCount(dbSuit.getCollectCount() - 1);
        apiSuitService.saveSuit(dbSuit);
        return new ApiResult(ResultMessage.RESULT_SUCCESS_1);
    }
}
